//
//  SearchResultViewController.m
//  RouteSearcher
//
//  Created by wenqingzhou on 2/12/11.
//  Copyright 2011 __MyCompanyName__. All rights reserved.
//

#import "SearchResultViewController.h"


@implementation SearchResultViewController

@synthesize myRRRM;
#pragma mark -
#pragma mark Initialization


- (id)initWithStyle:(UITableViewStyle)style {
    // Override initWithStyle: if you create the controller programmatically and want to perform customization that is not appropriate for viewDidLoad.
    self = [super initWithStyle:style];
    if (self) {
        self.tableView.rowHeight=50;
    }
    return self;
}


#pragma mark -
#pragma mark View lifecycle

/*
- (void)viewDidLoad {
    [super viewDidLoad];

    // Uncomment the following line to display an Edit button in the navigation bar for this view controller.
    // self.navigationItem.rightBarButtonItem = self.editButtonItem;
}
*/

/*
- (void)viewWillAppear:(BOOL)animated {
    [super viewWillAppear:animated];
}
*/
/*
- (void)viewDidAppear:(BOOL)animated {
    [super viewDidAppear:animated];
}
*/
/*
- (void)viewWillDisappear:(BOOL)animated {
    [super viewWillDisappear:animated];
}
*/
/*
- (void)viewDidDisappear:(BOOL)animated {
    [super viewDidDisappear:animated];
}
*/
/*
// Override to allow orientations other than the default portrait orientation.
- (BOOL)shouldAutorotateToInterfaceOrientation:(UIInterfaceOrientation)interfaceOrientation {
    // Return YES for supported orientations.
    return (interfaceOrientation == UIInterfaceOrientationPortrait);
}
*/


#pragma mark -
#pragma mark Table view data source

- (NSInteger)numberOfSectionsInTableView:(UITableView *)tableView {
    // Return the number of sections.
	return 1;
}


- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section {
    // Return the number of rows in the section.
	NSLog(@"numberofsections:%d",[self.myRRRM.arrayOfRoutes count]);
    return [self.myRRRM.arrayOfRoutes count];
}


// Customize the appearance of table view cells.
- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath {
        NSLog(@"getString,section:%d,row:%d",indexPath.section,indexPath.row);
    static NSString *CellIdentifier = @"anRouteDetails";
    
    UITableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:CellIdentifier];
    if (cell == nil) {
        cell = [[[UITableViewCell alloc] initWithStyle:UITableViewCellStyleDefault reuseIdentifier:CellIdentifier] autorelease];
    }
	 // Configure the cell..
	[cell addSubview:[self getRouteView:[self.myRRRM.arrayOfRoutes objectAtIndex:indexPath.row]]];
	[cell setAccessoryType:UITableViewCellAccessoryDetailDisclosureButton];
	return cell;
}

- (NSString *)tableView:(UITableView *)tableView titleForHeaderInSection:(NSInteger)section
{
	return [@"From:" stringByAppendingFormat:@"%@\nTo:%@",self.myRRRM.start
			,self.myRRRM.dest];
}
/*
// Override to support conditional editing of the table view.
- (BOOL)tableView:(UITableView *)tableView canEditRowAtIndexPath:(NSIndexPath *)indexPath {
    // Return NO if you do not want the specified item to be editable.
    return YES;
}
*/


/*
// Override to support editing the table view.
- (void)tableView:(UITableView *)tableView commitEditingStyle:(UITableViewCellEditingStyle)editingStyle forRowAtIndexPath:(NSIndexPath *)indexPath {
    
    if (editingStyle == UITableViewCellEditingStyleDelete) {
        // Delete the row from the data source.
        [tableView deleteRowsAtIndexPaths:[NSArray arrayWithObject:indexPath] withRowAnimation:UITableViewRowAnimationFade];
    }   
    else if (editingStyle == UITableViewCellEditingStyleInsert) {
        // Create a new instance of the appropriate class, insert it into the array, and add a new row to the table view.
    }   
}
*/


/*
// Override to support rearranging the table view.
- (void)tableView:(UITableView *)tableView moveRowAtIndexPath:(NSIndexPath *)fromIndexPath toIndexPath:(NSIndexPath *)toIndexPath {
}
*/


/*
// Override to support conditional rearranging of the table view.
- (BOOL)tableView:(UITableView *)tableView canMoveRowAtIndexPath:(NSIndexPath *)indexPath {
    // Return NO if you do not want the item to be re-orderable.
    return YES;
}
*/


#pragma mark -
#pragma mark Table view delegate

- (void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath {
    // Navigation logic may go here. Create and push another view controller.
    /*
    <#DetailViewController#> *detailViewController = [[<#DetailViewController#> alloc] initWithNibName:@"<#Nib name#>" bundle:nil];
     // ...
     // Pass the selected object to the new view controller.
    [self.navigationController pushViewController:detailViewController animated:YES];
    [detailViewController release];
    */
}


#pragma mark -
#pragma mark Memory management

- (void)didReceiveMemoryWarning {
    // Releases the view if it doesn't have a superview.
    [super didReceiveMemoryWarning];
    
    // Relinquish ownership any cached data, images, etc. that aren't in use.
}

- (void)viewDidUnload {
    // Relinquish ownership of anything that can be recreated in viewDidLoad or on demand.
    // For example: self.myOutlet = nil;
}

-(UIView *)getRouteView:(RouteModel *)myRoute
{
	UIView *myRowUiView=[[UIView alloc] initWithFrame:CGRectMake(0, 0, self.tableView.frame.size.width, self.tableView.rowHeight)];
	myRowUiView.autoresizesSubviews=YES;
	UILabel *startTime=[[UILabel alloc] initWithFrame:CGRectMake(0, 0, 40, self.tableView.rowHeight*0.6)];
	startTime.text=[[[[myRoute.arrayOfPath objectAtIndex:0] arrayOfPoints] objectAtIndex:0] departureTime];
	startTime.font=[UIFont systemFontOfSize:14];
	[startTime setTextAlignment:UITextAlignmentRight];
	[myRowUiView addSubview:startTime];
	[startTime release];
	UILabel *endTime=[[UILabel alloc] initWithFrame:CGRectMake(0, self.tableView.rowHeight*0.6, 40, self.tableView.rowHeight*0.4)];
	endTime.text=[[[[myRoute.arrayOfPath lastObject] arrayOfPoints] lastObject] departureTime];
	endTime.font=[UIFont systemFontOfSize:11];
	[endTime setTextAlignment:UITextAlignmentRight];
	[myRowUiView addSubview:endTime];
	[endTime release];
	NSLog(@"Call pathToView...");
	[myRowUiView addSubview:[self pathsToView:myRoute]];
	NSLog(@"pathToView success!");
	return [myRowUiView autorelease];
}
- (void)dealloc {
	NSLog(@"SRVC ENDING!");
	[self.myRRRM release];
    [super dealloc];
}

-(UIView *)pathsToView:(RouteModel *)myRoute
{
	UIView *myView= [[UIView alloc] initWithFrame:CGRectMake(40,0,250,self.tableView.rowHeight)];
	int xOffset=0;
	int yOffset=self.tableView.rowHeight*0.7;
	float walkDistance=0;
	for(PathModel *pm in myRoute.arrayOfPath)
	{
		UIImageView *imageView=[modelToView pathTypeToImageView:pm WithFrame:CGRectMake(xOffset,self.tableView.rowHeight*0.1,30,self.tableView.rowHeight*0.6)];
		[myView addSubview:imageView];
		UILabel *myLabel;
		if (pm.type==nil) {
			myLabel=[[modelToView stringToTextWithSmallFontCenterAlign:pm.dist WithFrame:CGRectMake(xOffset,yOffset,30,self.tableView.rowHeight*0.3)] retain];
			walkDistance+=[pm.dist floatValue];
		}
		else {
			myLabel=[[modelToView stringToTextWithSmallFontCenterAlign:pm.code WithFrame:CGRectMake(xOffset,yOffset,30,self.tableView.rowHeight*0.3)] retain];
		}
		xOffset=xOffset+30;
		[myView addSubview:myLabel];
		[myLabel release];
	}
	UILabel *durationLabel = [[modelToView stringToTextWithSmallFontCenterAlign:@"duration" WithFrame:CGRectMake(self.tableView.frame.size.width-150,self.tableView.rowHeight*0.1,80,self.tableView.rowHeight*0.3)] retain];
	UILabel *durationValueLabel = [[modelToView stringToTextWithLargeFontCenterAlign:[NSString stringWithFormat:@"%dMIN",myRoute.duration] WithFrame:CGRectMake(self.tableView.frame.size.width-150,self.tableView.rowHeight*0.4,80,self.tableView.rowHeight*0.5)] retain];
	[myView addSubview:durationLabel];
	[myView addSubview:durationValueLabel];
	[durationLabel release];
	[durationValueLabel release];
	return [myView autorelease];	
}

- (void)tableView:(UITableView *)tableView accessoryButtonTappedForRowWithIndexPath:(NSIndexPath *)indexPath
{
	RouteDetailViewController *rdvc=[[RouteDetailViewController alloc] initWithStyle:UITableViewStylePlain];
	rdvc.route=[self.myRRRM.arrayOfRoutes objectAtIndex:indexPath.row];
	[self.navigationController pushViewController:rdvc animated:YES];
	[rdvc release];
}


@end

